﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace BattleCity
{
    public static class Sound
    {
        //private static Song menuSong;
        private static Song menuSong;
        private static Song gamePlaySong;

        public static SoundEffect shoot;
        public static SoundEffect buttonPressed;
        public static SoundEffect destroyedTank;
        public static SoundEffect playerSpawn;
        public static SoundEffect bonusCatched;

        static SoundEffectInstance[] soundEffect = new SoundEffectInstance[3];
        static SoundEffectInstance soundEffectBonus;

        static int instanceCounter = 0;

        public static void Intialize(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            menuSong = content.Load<Song>(@"sounds\spacemenu");
            gamePlaySong = content.Load<Song>(@"sounds\battlesong");
            shoot = content.Load<SoundEffect>(@"sounds\shoot_fx");
            destroyedTank = content.Load<SoundEffect>(@"sounds\destroyed_fx");
            playerSpawn = content.Load<SoundEffect>(@"sounds\generated_fx");
            bonusCatched = content.Load<SoundEffect>(@"sounds\bonus_fx");
            buttonPressed = content.Load<SoundEffect>(@"sounds\button_pressed");
        }

        public static void startMenuMusic()
        {
            if ((MediaPlayer.GameHasControl) && (Options.soundEnabled))
            {
                MediaPlayer.Volume = 1.0f;
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(menuSong);
            }
        }

        public static void startGamePlayMusic()
        {
            if ((MediaPlayer.GameHasControl) && Options.soundEnabled)
            {
                MediaPlayer.Stop();
                MediaPlayer.Play(gamePlaySong);
            }
        }

        public static void stopMusic()
        {
            if ((MediaPlayer.GameHasControl))
            {
                MediaPlayer.Stop();
            }
        }

        public static void stopSoundsFx()
        {
            if (soundEffect[0] != null)  Sound.soundEffect[0].Stop();
            if (soundEffect[1] != null)   Sound.soundEffect[1].Stop();
            if (soundEffect[2] != null)   Sound.soundEffect[2].Stop();

            if (soundEffectBonus != null) Sound.soundEffectBonus.Stop();
        }

        public static void playSound(ref SoundEffect sound)
        {
            if ((MediaPlayer.GameHasControl))
            {
                soundEffect[instanceCounter] = sound.CreateInstance();
                soundEffect[instanceCounter].Play();

                instanceCounter++;
                if (instanceCounter > 2) instanceCounter = 0;
            }
        }

        public static void playBonusSound()
        {
            if ((MediaPlayer.GameHasControl))
            {
                soundEffectBonus = bonusCatched.CreateInstance();
                soundEffectBonus.Play();
                bonusCatched.Play();
            }
        }
    }
}
